package com.settlement.system.service.impl;

import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblAkcDifferentReportMapper;
import com.settlement.system.mapper.TblReceiveRefundRateMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblReceiveRefundRate;
import com.settlement.system.service.ITblReceiveRefundRateService;
import com.settlement.system.service.ITblUnificationBillDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收退款-比率表 服务实现类
 *
 * @author Ximi
 * @since 2020-06-30
 */
@Service
@Slf4j
public class TblReceiveRefundRateServiceImpl extends BaseServiceImpl implements ITblReceiveRefundRateService {

    @Resource
    private TblReceiveRefundRateMapper tblReceiveRefundRateMapper;

    @Resource
    private TblAkcDifferentReportMapper tblAkcDifferentReportMapper;

    @Resource
    private ITblUnificationBillDetailService tblUnificationBillDetailService;

    @Override
    public BaseMapper init() {
        return tblReceiveRefundRateMapper;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public Integer batchUpdatefinanceData(List<TblReceiveRefundRate> list) throws ServiceException {
        try {
            Integer updateCount = 0;
            for (TblReceiveRefundRate info : list) {
                updateCount += tblReceiveRefundRateMapper.updateImportByActionIdBillType(info);
            }
            return updateCount;
        } catch (Exception ex) {
            throw new ServiceException("根据活动ID,类型批量财务数据异常", ex);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public <ModelType> Integer saveNoDuplicateList(List<ModelType> list) throws ServiceException, DuplicateKeyException {
        for(ModelType modelType:list){
            TblReceiveRefundRate info = (TblReceiveRefundRate) modelType;
            TblReceiveRefundRate temp = tblReceiveRefundRateMapper.getRate(info.getActionId(),info.getBillType());
            if(temp == null){
                tblReceiveRefundRateMapper.insert(info);
            }else {
                tblReceiveRefundRateMapper.updateRobotByActionIdBillType(info);
            }

            if(temp != null){
                info.setPermitDifferentComfire(temp.getPermitDifferentComfire());
            }

            boolean flag = (StringUtils.isEmpty(info.getPermitDifferentComfire()) || "N".equals(info.getPermitDifferentComfire()))
                    && info.getRobotComfirmDate() != null
                    && "首款".equals(info.getBillType());
            if(flag){
                tblAkcDifferentReportMapper.deleteByActionId(info.getActionId());
            }
        }
        return list.size();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public <ModelType> Integer update(ModelType modelType) throws ServiceException {
        tblReceiveRefundRateMapper.update(modelType);
        TblReceiveRefundRate info = (TblReceiveRefundRate) modelType;
        TblReceiveRefundRate rate = null;
        Map<String, Object> map = new HashMap<String,Object>(16);
        map.put("id",info.getId());
        List<TblReceiveRefundRate> rateList = tblReceiveRefundRateMapper.selectByParam(map);
        if(rateList!=null && !rateList.isEmpty()){
            rate = rateList.get(0);
        }
        if(rate !=null && rate.getExtractMoneyDate() != null){
            tblUnificationBillDetailService.updateAkcDate(rate.getActionId(),rate.getBillType(),rate.getExtractMoneyDate());
        }
        tblUnificationBillDetailService.refreshNoMatchByActionIdOfAkc(rate.getActionId());
        return 0;
    }
}

